from django.shortcuts import render, redirect
from django import forms
from SystemApp import models
class Systemapp_modelform(forms.ModelForm):
    class Meta:
        model=models.assetconting
        fields='__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, filed in self.fields.items():
            filed.widget.attrs = {"class": "form-control","style":"margin-bottom: 10px"}

def index(request):
    if request.method=='GET':
        form =Systemapp_modelform()
        return render(request,'index.html',{'form':form})
    form=Systemapp_modelform(request.POST,request.FILES)
    if form.is_valid():
        form.save()
        return render(request, "index.html",locals())

def edits(request, nid):
    if request.method == "GET":
        names = models.assetconting.objects.filter(id=nid).first()

        return render(request, "edits.html", {"names": names})
    else:
        property_number = request.POST.get("property_number")
        location=request.POST.get("location")
        models.assetconting.objects.filter(id=nid).update(property_number=property_number,Location=location)
        return redirect("/look/")

# def look(request):
#     form=models.assetconting.objects.all()
#     return render(request,'views.html',{'form':form})

def look(request):
    data_dict={}
    search_data=request.GET.get('q')
    if search_data:
        data_dict['property_number__contains']=search_data
    res=models.assetconting.objects.filter(**data_dict).order_by('-property_number')
    return render(request,'look.html',{"res":res})

def deletes(request):
    nid = request.GET.get("nid")
    models.assetconting.objects.filter(id=nid).delete()
    return redirect('/look/')






def layout(request):
    return render(request,'layout.html',locals())